package com.cilico.utils;


import android.app.Activity;
import android.graphics.Bitmap;

import com.cilico.R;

public class Constant {
	public static final String HMAC_VERIFY_ERROR = "网络异常，请退出重新登录。";
	public final static String ERR_PARSE_XML_01 = "数据解析失败。";
	public final static String ERR_PARSE_XML_02 = "数据解析失败。请咨询400-830-8003。";
	public final static String ERR_DATA_FORMAT = "错误的数据格式。";
	public final static String ERR_PARSE_CHANNEL = "解析频道出错。";
	public final static String ERR_ACCOUNT_LIST = "卡号列表错误。";
	public final static String ERR_BASE64 = "网络繁忙，请稍候。EMP_C_64";
	public final static String ERR_NULL_POI = "没有相关内容。";
	public final static String ERR_NULL_CHANNEL = "无法取得相关内容。";
	public final static String ERR_ENCRYPT_SERVER_01 = "通讯异常。请咨询400-830-8003。";
	public final static String ERR_ENCRYPT_SERVER_02 = "暂时无法获取服务器的响应。";
	public final static String ERR_CONNECTION_FAIL = "无法连接服务器。";
	public final static String ERR_NO_GATEWAY = "通信异常，请检查您的网络设置及通讯情况!";
	public final static String ERR_GENERAL_EXCEPTION = "通讯异常。请咨询400-830-8003。";
	public final static String ERR_SYSTEM_RES = "系统资源不足，请退出重新登录。";
	public final static String ERR_NO_RESULT = "无法正确读取数据。";
	public final static String ERR_GUNZIP_DATA_FORMAT = "压缩数据格式错误。";
	public final static String ERR_GUNZIP_FAIL = "不能正确解压数据。";
	public final static String ERR_DOWNLOAD_DISCONN = "下载数据时网络被中断。";
	public final static String ERR_NULL_XML = "网络繁忙，请稍候。";
	public final static String ERR_INVALID_POSITION = "无效的经度和纬度。";
	public final static String ERR_POSITION_FORMAT = "位置格式错误。";
	public final static String ERR_NO_POSITION = "位置无效。";
	public final static String ERR_POS_NOT_FOUND = "找不到地址。";
	public final static String ERR_RMS_FULL = "RMS存储空间不足。";
	public final static String ERR_NETWORK_BUSY = "网络繁忙。";
	public final static String ERR_LOGIN_NO_RESPONSE = "无法获取服务器登录响应。";
	public static final String SERVER_ERROR = "服务器暂时不能使用.";
	public static final String ERR_LOGIN_UNKNOWN = "登录失败！未知的响应状态。";
	public static final String ERR_LOGIN_FAILURE = "登录失败，请重试。";
	public static final String MAIN_TITLE = null;
	Activity activity_;
	/* main view icons */
	static final String DEFAULT_CHANNEL_SHORTCUT = "custom";
	public static final String DEFAULT_CHANNEL_SHORTCUT_MINI = "customMini";
	public final static String FIRSTPAGE = "首页"; // 状态栏"首页"
	Bitmap mbButton_;
	Bitmap exitIcon_;
	public boolean isAtLeft_ = false;
	public static Bitmap arrSelect_; // Select控件图片
	public static Bitmap activeTab_; // TabBar控件图片
	public static Bitmap inactiveTab_;
	public static Bitmap backgroundTab_;
	public static Bitmap arrTab_;
	public static Bitmap statusBackground_; // 状态栏StatusBar控件中显示的背景图片
	public static Bitmap TABLEARR; // 表格中显示的箭头图片

	public Constant(Activity activity) {
		activity_ = activity;
		// 创建Select控件图片
		arrSelect_ = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.select_arr);
		// 创建TabBar控件图片
		backgroundTab_ = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.tabbarbg);
		// 状态栏StatusBar控件中显示的背景图片
		statusBackground_ = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.statusbg);
		// 表格中显示的箭头图片
		TABLEARR = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.arr);
	}

	public Bitmap getChannelIcon(String shortcut) {
		Bitmap icon = null;
		if (shortcut != null && !shortcut.equals("")) {
			icon = createChannelIcons(shortcut);
		}
		return icon;
	}

	public Bitmap getChannelIconMini(String shortcut) {
		Bitmap icon = null;
		if (shortcut != null && !shortcut.equals("")) {
			Object obj = createChannelMiniIcons(shortcut);
			if (obj != null) {
				icon = (Bitmap) obj;
			}
		}
		return icon;
	}

	public Bitmap getFocusChannelIcon(String shortcut) {
		Bitmap icon = null;
		if (shortcut != null && !shortcut.equals("")) {
			Object obj = createChannelMiniIconsSelected(shortcut);
			if (obj != null) {
				icon = (Bitmap) obj;
			}
		}
		return icon;
	}

	/**
	 * Makes the text of the button in the main view fit.
	 */
	public String fitText(final String text) {
		String fitText = text;
		try {
			if (fitText.length() == 2) {
				fitText = fitText.charAt(0) + " " + fitText.charAt(1);
			}
		} catch (Exception ex) {
			fitText = text;
		}
		return fitText;
	}

	private Bitmap createChannelIcons(String name) {
		Bitmap icon = null;
		try {

			if (activity_.getString(R.string.naviitem1).equals(name)) {
				// 帮助
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.main6);
			} else if (activity_.getString(R.string.naviitem2).equals(name)) {
				// 优惠信息
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.main2);
			} else if (activity_.getString(R.string.naviitem3).equals(name)) {
				// 登陆
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.main1);
			} else if (activity_.getString(R.string.naviitem4).equals(name)) {
				// 我的绿卡
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.main5);
			} else if (activity_.getString(R.string.naviitem5).equals(name)) {
				// 点餐
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.main3);
			} else if (activity_.getString(R.string.naviitem6).equals(name)) {
				// 个人设置
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.main4);
			} else if (activity_.getString(R.string.navihome).equals(name)) {
				// 首页
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitemhome);
			} else {
				// 默认为帮助
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.main6);
			}

		} catch (Exception ex) {
			Utils.printException(ex);
		}

		return icon;
	}

	private Bitmap createChannelMiniIcons(String name) {
		Bitmap icon = null;
		try {
			if (activity_.getString(R.string.naviitem1).equals(name)) {
				// 帮助
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem1);
			} else if (activity_.getString(R.string.naviitem2).equals(name)) {
				// 优惠信息
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem2);
			} else if (activity_.getString(R.string.naviitem3).equals(name)) {
				// 登陆
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem3);
			} else if (activity_.getString(R.string.naviitem4).equals(name)) {
				// 我的绿卡
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem4);
			} else if (activity_.getString(R.string.naviitem5).equals(name)) {
				// 点餐
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem5);
			} else if (activity_.getString(R.string.naviitem6).equals(name)) {
				// 个人设置
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem6);
			} else if (activity_.getString(R.string.navihome).equals(name)) {
				// 首页
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitemhome);
			} else {
				// 默认为帮助
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem1);
			}
		} catch (Exception ex) {
			Utils.printException(ex);
		}

		return icon;
	}

	private Bitmap createChannelMiniIconsSelected(String name) {
		Bitmap icon = null;
		try {
			if (activity_.getString(R.string.naviitem1).equals(name)) {
				// 帮助
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem1_s);
			} else if (activity_.getString(R.string.naviitem2).equals(name)) {
				// 优惠信息
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem2_s);
			} else if (activity_.getString(R.string.naviitem3).equals(name)) {
				// 登陆
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem3_s);
			} else if (activity_.getString(R.string.naviitem4).equals(name)) {
				// 我的绿卡
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem4_s);
			} else if (activity_.getString(R.string.naviitem5).equals(name)) {
				// 点餐
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem5_s);
			} else if (activity_.getString(R.string.naviitem6).equals(name)) {
				// 个人设置
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem6_s);
			} else if (activity_.getString(R.string.navihome).equals(name)) {
				// 首页
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitemhome);
			} else {
				// 默认为帮助
				icon = ConstantRepository.decodeBitmap(activity_.getResources(), R.drawable.naviitem1_s);
			}
		} catch (Exception ex) {
			Utils.printException(ex);
		}

		return icon;
	}

	public Bitmap[] getArrowIcon(Bitmap imgArr, int width, int height) {
		Bitmap[] icon = null;
		Bitmap[] arrowIcons = createArrowIcon(imgArr);
		int size = arrowIcons != null ? arrowIcons.length : 0;
		if (size > 0) {
			icon = new Bitmap[size];
		}
		for (int i = 0; i < size; i++) {
			icon[i] = Bitmap.createScaledBitmap(arrowIcons[i], width, height, true);
		}
		return icon;
	}

	private Bitmap[] createArrowIcon(Bitmap imgArr) {
		Bitmap[] arrowIcons = null;
		try {
			// 原图未选中时的RGB
			int dataLeftOff[];
			// 原图选中时的RGB
			int dataLeftOn[];
			// 反转后未选中时的RGB
			int dataRightOff[];
			// 反转后选中时的RGB
			int dataRightOn[];

			Bitmap temp = imgArr;
			int eachWidth = temp.getWidth() / 2;
			int height = temp.getHeight();

			dataLeftOff = new int[eachWidth * height];
			dataLeftOn = new int[eachWidth * height];
			dataRightOff = new int[eachWidth * height];
			dataRightOn = new int[eachWidth * height];

			Bitmap leftOn = Bitmap.createBitmap(temp, 0, 0, eachWidth, height);
			Bitmap leftOff = Bitmap.createBitmap(temp, eachWidth, 0, eachWidth, height);
			leftOn.getPixels(dataLeftOn, 0, eachWidth, 0, 0, eachWidth, height);
			leftOff.getPixels(dataLeftOff, 0, eachWidth, 0, 0, eachWidth, height);
			// 像素移动的目的位置
			int destIndex;
			// 读取像素的位置
			int orIndex;
			for (int i = 0; i < height; i++) {
				// 单个像素对应的二维行位置
				int numIndex = i * eachWidth;

				for (int j = 0; j < eachWidth; j++) {
					destIndex = numIndex + j;
					orIndex = numIndex + eachWidth - j - 1;
					dataRightOff[destIndex] = dataLeftOff[orIndex];
					dataRightOn[destIndex] = dataLeftOn[orIndex];
				}
			}
			Bitmap rightOff = Bitmap.createBitmap(dataRightOff, eachWidth, height, Bitmap.Config.ARGB_8888);
			Bitmap rightOn = Bitmap.createBitmap(dataRightOn, eachWidth, height, Bitmap.Config.ARGB_8888);
			arrowIcons = new Bitmap[4];
			arrowIcons[0] = leftOn;
			arrowIcons[1] = leftOff;
			arrowIcons[2] = rightOn;
			arrowIcons[3] = rightOff;
		} catch (Exception ex) {
			Utils.printException(ex);
		}

		return arrowIcons;
	}

}
